package com.example.children.controller;

import com.example.children.Exception.LoginException;
import com.example.children.Util.JwtUtil;
import com.example.children.Util.Tools;
import com.example.children.Util.tool;
import com.example.children.annotation.AllowGuestAccess;
import com.example.children.Model.User;
import com.example.children.Model.ResponseModel;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

    @RestController
    @RequestMapping("/Dashboard")  //类名
    public class Dashboard  extends BaseServlet{

        // 存储用户名和密码的映射
        public static Map<String, String> userDatabase = new HashMap<>();

        static {
            // 假设这是预先存储的用户数据
            userDatabase.put("admin", "123");
            userDatabase.put("yyz", "123");
            userDatabase.put("yan", "123");
        }

        @AllowGuestAccess
        @PostMapping("/dashboard") //方法名
        public ResponseModel<Map<String, String>> login(@RequestBody User user){

            System.out.println("进入login方法："+user.getUsername());
            Integer code= Tools.Success;
            String msg= tool.success;
            String accessToken =null;
            String refreshToken =null;
//    ResponseModel<String> tt=new ResponseModel<String>(code,msg,null);

            Map<String, String> toKenMap =new HashMap();
            String storedPassword = userDatabase.get(user.getUsername());
            if (storedPassword != null && storedPassword.equals(user.getPassword())) {
                System.out.println("Yes");
//        生成jwt
                accessToken= JwtUtil.generateToken(user.getId(),1000*5,"user");
                refreshToken= JwtUtil.generateToken(user.getId(),1000*10,"user");
//        System.out.println("accessToken:"+accessToken);
//        System.out.println("refreshToken:"+refreshToken);
                toKenMap.put("accessToken",accessToken);
                toKenMap.put("refreshToken",refreshToken);
            }
            else{
                System.out.println("No");
                throw new LoginException("用户名或密码错误");
            }
            return new ResponseModel<>(code,msg,toKenMap);//响应给前端的对象
        }

        @PostMapping("/guestLogin")  //游客登录
        public ResponseModel<Map<String, String>> guestLogin(@RequestBody User user){
            System.out.println("进入guestLogin方法："+user.getUsername());
            Integer code= Tools.Success;
            String msg= tool.success;
            String accessToken =null;
            String refreshToken =null;
            Map<String, String> toKenMap =new HashMap();
            System.out.println("Yes");
            accessToken= JwtUtil.generateToken(user.getId(),1000*5,"guest");
            refreshToken= JwtUtil.generateToken(user.getId(),1000*10,"guest");
            toKenMap.put("accessToken",accessToken);
            toKenMap.put("refreshToken",refreshToken);
            return new ResponseModel<>(code,msg,toKenMap);//响应给前端的对象
        }


        // 获取用户数据的方法
        public static String getPasswordForUser(String username) {
            return userDatabase.get(username);
        }

        // 添加用户的方法
        public static void addUser(String username, String password) {
            userDatabase.put(username, password);
        }

        // 检查用户是否存在
        public static boolean userExists(String username) {
            return userDatabase.containsKey(username);
        }




    }


